********************************************************************************
** Do file: g11_control_repayment.do
** First started: March 15, 2023
** Last edited: September 8, 2023

/* 
Part I: Over what time period did 9% of control households have a school fee loan?
*/

********************************************************************************

clear
clear matrix
clear mata
set maxvar 10000

*** Preamble - make data sets ***

* Load repayment dataset
use "$repay_clean/fenix_repay_extend_07172020_rep.dta", clear

* Note that this is referring to endline surveyed 
preserve
	use "$merged/key_rep.dta", clear
	keep if hhid!=.

	* Restrict down to sample of interest
	keep if k_complete_may==1 & k_rolling_list==1  // & k_interacted_success==1 & k_surveyed==1 & k_surveyed_end==1

	* Drop the choice group as well
	drop if treatmenttype_sh=="R T3"

	* Develop indicators
	g anytreat_actual = (k_tookloan_repay==1)
	g locked_actual = (k_tookloan_repay==1 & treatmenttype_sh=="R T1-L")
	g surprise_actual = (k_tookloan_repay==1 & treatmenttype_sh=="R T1-U")
	g unlocked_actual = (k_tookloan_repay==1 & treatmenttype_sh=="R T2-U")

	g anytreat_assigned = (treatmenttype_sh=="R T1-L" | treatmenttype_sh=="R T1-U" | treatmenttype_sh=="R T2-U") 
	g locked_assigned = (treatmenttype_sh=="R T1-L")
	g surprise_assigned = (treatmenttype_sh=="R T1-U")
	g unlocked_assigned = (treatmenttype_sh=="R T2-U")

	*sum anytreat_actual if anytreat_assigned==0
	sum anytreat_actual if anytreat_assigned==0 & k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_surveyed==1 & k_surveyed_end==1
	codebook accountid if anytreat_assigned==0 & k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_surveyed==1 & k_surveyed_end==1 & anytreat_actual==1 & k_tookloan_repay==1 // 30
restore

* Merge in variables from key dataset to filter down to appropriate sample
merge m:1 accountid using "$merged/key_rep.dta", keepusing(k_complete_may k_rolling_list k_interacted_success k_tookloan_repay k_surveyed k_surveyed_end)
keep if _merge==1 | _merge==3
drop _merge

* Note: this is surveyed sample (up to endline)
g tag = 1 if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & k_surveyed==1 & k_surveyed_end==1 & treatmenttype_sh=="R C"

* How many loans are there?
codebook accountid if tag==1

* Examine just based on the filter developed directly above
keep if tag==1

************
** Part I **
************

tab loandayselapsed // everyone has at least a year of data
tab loanbenchmarkdate if loandayselapsed==0 // when did people begin their loans? similar to expected
collapse (mean) frac_lpp_maxip, by(loandayselapsed) // repayment over loan days for control group
